﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace HIS.Utils {
    public class UploadHandler : IHttpHandler {
        #region IHttpHandler Members

        public bool IsReusable {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context) {
            try {
                //Uploaded File Deletion
                if (context.Request.QueryString.Count > 0) {
                    string filePath = HttpContext.Current.Request.PhysicalApplicationPath + "Images\\PatientImages\\" + context.Request.QueryString[0].ToString(); 
                    if (File.Exists(filePath))
                        File.Delete(filePath);
                }
                    //File Upload
                else {
                    string fileName = Path.GetFileName(context.Request.Files[0].FileName);
                    string[] concatStr = fileName.Split('.');
                    string fileExt = concatStr[concatStr.Length - 1];
                    Guid msgGuid = System.Guid.NewGuid();
                    fileName= DateTime.Today.ToString("dd-MM-yy") + "-" + msgGuid;
                    string location = HttpContext.Current.Request.PhysicalApplicationPath + "Images\\PatientImages\\" + fileName+"."+fileExt;
                    context.Request.Files[0].SaveAs(location);
                }
            } catch {

            }
        }

        #endregion
    }
}